﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using WeBlog.Interfaces;
using WeBlog.Repositories;

namespace WeBlog.Models
{
    public class ManageCommentsViewModel : GridModel<PostComment>
    {        
        public int[] CommentIds { get; set; }
        public int SelectedStatus { get; set; }
        public string Query { get; set; }
        private CommentRepository _repository;
             
        public ManageCommentsViewModel(IUnitOfWork unitOfWork, GridSettings settings )
            : base( settings )
        {
            _repository = new CommentRepository(unitOfWork);
            SelectedStatus = (int)CommentStatuses.Any;            
            Settings.DefaultPageSize = 25;
            Settings.DefaultSortColumn = "DateTime";
        }

        public IEnumerable<SelectListItem> Statuses { 
            get {
                return from object status in Enum.GetValues(typeof(CommentStatuses)) 
                       select new SelectListItem {Text = status.ToString(), Value = ((int) status).ToString()};
            }
        }

        public override IQueryable<PostComment> GetSuperSet()
        {
            var query = _repository.FindAll();

            if (!String.IsNullOrWhiteSpace(Query))
            {
                query = query.Where(x => x.Content.Contains(Query) ||
                                x.Email.Contains(Query) ||
                                x.Post.Title.Contains(Query));
            }

            if (SelectedStatus != (int)CommentStatuses.Any)
                return query.Where(x => x.CommentStatus == SelectedStatus);

            return query;
        }
    }    
}